package online.foxnull2.foxmall.member.interceptor;

import online.foxnull2.common.constant.AuthConstant;
import online.foxnull2.common.vo.MemberRespVo;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVo> threadLocal = new InheritableThreadLocal<>();

    @Override
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) {
//        String uri = request.getRequestURI();
//        AntPathMatcher antPathMatcher = new AntPathMatcher();
//        if (antPathMatcher.match("/member/member/login", uri) ||
//                antPathMatcher.match("/member/member/register", uri) ||
//                antPathMatcher.match("/member/memberreceiveaddress/info/*", uri)) {
//            return true;
//        }
        HttpSession session = request.getSession();
        MemberRespVo memberRespVo = (MemberRespVo) session.getAttribute(AuthConstant.LOGIN_USER);
        if (!ObjectUtils.isEmpty(memberRespVo)) {
            threadLocal.set(memberRespVo);
//            return true;
//        } else {
//            session.setAttribute("msg", "Please Login");
//            response.sendRedirect("http://auth.foxmall.com/login.html");
//            return false;
        }
        return true;
    }
}
